--- zrob plik konfiguracyjny i zaladuj go uzywajac dofile()
chestTable = {
	[1] = {chest1 = 5948, chest2 = 2301, chest3 = 2365, count2 = 5}, --- red dragon leather, fire field rune
	[2] = {chest1 = 2156, chest2 = 2795, count2 = 5, chest3 = 2365}, --- red gem, fire mushrooms, v rod [nie istnieje]
	[3] = {chest1 = 2363, chest2 = 2546, count2 = 5, chest3 = 2200}, -- blood orb, burst arrow, protection amulet
	[4] = {chest1 = 2365, chest2 = 2201, chest3 = 5882} --- backpack of holding, dragon necklace, red dragon scale
}

chestTable[5] = chestTable[1]
chestTable[6] = chestTable[2]
chestTable[7] = chestTable[3]
chestTable[8] = chestTable[4]

enter_pos = {x = 272, y = 134, z = 9}
storage = 1111

itemPos = {{x = 362, y = 126, z = 10},{x = 362, y = 127, z = 10},{x = 362, y = 128, z = 10}}

function teleportBlockingPlayer(cid, pos, pos2)
	if not isPlayer(cid) then
		return false
	end
	local playerPos = getThingPos(cid)
	if playerPos.x == pos.x and playerPos.y == pos.y and playerPos.z == pos.z then
		doTeleportThing(cid, pos2)
	end
	return true
end

--- create items
function onStepIn(cid, item, position, lastPosition)
	if getPlayerStorageValue(cid, storage) == 1 then
		doSendMagicEffect(position, CONST_ME_FIREATTACK)
		doTeleportThing(cid, enter_pos)
		return false
	end
	
	addEvent(teleportBlockingPlayer, 60*1000, cid, position, {x = position.x -6, y = position.y, z = position.z})

	local TABLE = chestTable[getPlayerVocation(cid)]
	for a = 1, table.maxn(itemPos) do
		item_ = getThingFromPos({x = (itemPos[a]).x, y = (itemPos[a]).y, z = (itemPos[a]).z, stackpos = 255})
		if item_.uid ~= 0 and item_.itemid ~= 2605 then --- don't remove bassin
			doRemoveItem(item_.uid)
		end
		doSendMagicEffect(itemPos[a], CONST_ME_FIREATTACK) --- wtf ?
	end

	if TABLE.count1 == nil then
		TABLE.count1 = 1
	end
	if TABLE.count2 == nil then
		TABLE.count2 = 1
	end
	if TABLE.count3 == nil then
		TABLE.count3 = 1
	end
	doCreateItem(TABLE.chest1, TABLE.count1, {x= (itemPos[1]).x, y= (itemPos[1]).y, z = (itemPos[1]).z})
	doCreateItem(TABLE.chest2, TABLE.count2, {x= (itemPos[2]).x, y= (itemPos[2]).y, z = (itemPos[2]).z})
	doCreateItem(TABLE.chest3, TABLE.count3, {x= (itemPos[3]).x, y= (itemPos[3]).y, z = (itemPos[3]).z})
	
	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have one minute to get reward.")

	return true
end

--- destroy items
function onStepOut(cid, item, position, fromPosition)
	for _, t_ in ipairs(itemPos) do
		for i = 1, 255 do
			item_ = getThingFromPos({x = t_.x, y = t_.y, z = t_.z, stackpos = 255})
			if item_.uid == 0 or item_.itemid == 2605 then
				doSendMagicEffect({x = t_.x, y = t_.y, z = t_.z}, CONST_ME_FIREATTACK)
				break
			end
			doRemoveItem(item_.uid)
		end
	end
	doSendMagicEffect(fromPosition, CONST_ME_FIREATTACK)
	return true
end
